package com.lanou.wuyou.gift.list;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.ListBean;
import com.lanou.wuyou.gift.tools.OnItemClickListener;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/16.
 */

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnItemClickListener{
    private Context context;
    private ListBean listBean;
    private OnItemClickListener clickListener;

    public void setListBean(ListBean listBean) {
        this.listBean = listBean;
        notifyDataSetChanged();
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public ListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                View title = LayoutInflater.from(context).inflate(R.layout.item_list_title, parent, false);
                viewHolder = new TitleViewHolder(title);
                break;
            case 1:
                View list = LayoutInflater.from(context).inflate(R.layout.item_list_recyler_view, parent, false);
                viewHolder = new ContentHolder(list);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.CLick(v,position);
            }
        });
        if (listBean != null){
            if (getItemViewType(position) == 0) {
                    TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
                    ListBean.DataBean dataBean = listBean.getData();
                    if (dataBean != null) {
                        Picasso.with(context).load(dataBean.getCover_image()).into(titleViewHolder.titleIv);
                        titleViewHolder.titleIv.setAdjustViewBounds(true);
                    }
            }else{
                ContentHolder contentHolder = (ContentHolder) holder;
                ListBean.DataBean.ItemsBean itemsBean =  listBean.getData().getItems().get(position - 1 );
                if (itemsBean != null) {
                    Picasso.with(context).load(itemsBean.getCover_image_url()).into(contentHolder.coverIv);
                    contentHolder.titleTv.setText(itemsBean.getName());
                    contentHolder.introductionTv.setText(itemsBean.getShort_description());
                    contentHolder.moneyTv.setText(itemsBean.getPrice());
                }
                }
        }
    }

    @Override
    public int getItemCount() {
        if (listBean == null){
            return 0;
        }
        if (listBean.getData() == null){
            return 0;
        }
        return listBean.getData().getItems() != null && listBean.getData().getItems().size() > 0 ? listBean.getData().getItems().size() : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public void CLick(View view, int position) {

    }

    @Override
    public void onClick(View v) {

    }

    class TitleViewHolder extends RecyclerView.ViewHolder{
        ImageView titleIv;
        public TitleViewHolder(View itemView) {
            super(itemView);
            titleIv = (ImageView) itemView.findViewById(R.id.title_recycler);
        }
    }
    class ContentHolder extends RecyclerView.ViewHolder{
        ImageView coverIv;
        TextView titleTv,introductionTv,moneyTv;
        public ContentHolder(View itemView) {
            super(itemView);
            coverIv = (ImageView) itemView.findViewById(R.id.cover_recycler);
            titleTv = (TextView) itemView.findViewById(R.id.title_recycler_tv);
            introductionTv = (TextView) itemView.findViewById(R.id.introduction_recycler);
            moneyTv = (TextView) itemView.findViewById(R.id.money_recycler);
        }
    }
}
